package com.soujiayi.activity;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.soujiayi.coupon.Utils;



public class CouponDetailActivity extends Activity {
    private ImageView imgDetailHome;
    private Button butDetailBack;
    private TextView tvDetailTitle;
    private ImageView imgDtailIcon;
    private TextView tvDetailFree;
    private TextView tvDetailFrom;
    private TextView tvDetailMore;
    private Button butDetailClickGet;
	private ProgressDialog pd;
	private boolean flag;//记录是否领取
	private Activity mActivity=CouponDetailActivity.this; 
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		((MyApplication) this.getApplication()).addActivity(this);
		setContentView(R.layout.coupon_detail);
	
		initViewByOrder();
		initActionListener();
		
		
	}

	public void initViewByOrder()
	{
		     flag=false;
		 	 mActivity.getIntent();//获取url信息
             imgDetailHome=(ImageView)findViewById(R.id.img_coupon_detail_photo);
		     butDetailBack=(Button)findViewById(R.id.bt_coupon_detail_back);
		     tvDetailTitle=(TextView)findViewById(R.id.tv_coupon_detail_title);
		     imgDtailIcon=(ImageView)findViewById(R.id.img_coupon_detail_icon);
		     tvDetailFree=(TextView)findViewById(R.id.tv_coupon_detail_free);
		     tvDetailFrom=(TextView)findViewById(R.id.tv_coupon_detail_from);
		     tvDetailMore=(TextView)findViewById(R.id.tv_coupon_detail_more);
		     butDetailClickGet=(Button)findViewById(R.id.but_coupon_clickget);
	
	}
	public void initActionListener()
	{
		imgDetailHome.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent backHome=new Intent(CouponDetailActivity.this,IndexActivity.class);
					startActivity(backHome);	
					
				
			          }
		    });
		butDetailBack.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					CouponDetailActivity.this.finish();
				}
	       });
		butDetailClickGet.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(flag==false){
			    if(SoujiayiLoginActivity.couponID == null){
			    	
						AlertDialog.Builder builder = new AlertDialog.Builder(CouponDetailActivity.this);
						builder.setTitle(getResources().getString(R.string.prompt)).setMessage(getResources().getString(R.string.prompt_no_login))
						.setPositiveButton(getResources().getString(R.string.qd), new OnClickListener() {
							
							@Override
							public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
								Intent intent = new Intent(CouponDetailActivity.this , SoujiayiLoginActivity.class);
								CouponDetailActivity.this.startActivity(intent);
								
							}
						});
						AlertDialog alertDialog = builder.create();
						alertDialog.show();
						return;
					}
			    //此处判断用户数据库中是否已经存在该优惠券
					pd = Utils.showPd(mActivity, pd, getResources().getString(R.string.prompt_add));
					
			      	//登录之后的返回到领取优惠卷的界面，并显示已经领取成功，得到返回码
				//	String url = Url.couponUrl+ "?customerID=" ;
				
			      	Toast.makeText(mActivity, "已成功领取", Toast.LENGTH_SHORT).toString();
				    flag=true;
				}
				else
				{
                    AlertDialog.Builder builder = new AlertDialog.Builder(CouponDetailActivity.this);
					builder.setTitle(getResources().getString(R.string.prompt)).
					setMessage(getResources().getString(R.string.not_get_again))
					.setPositiveButton(getResources().getString(R.string.qd), new OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
						}
					});
					AlertDialog alertDialog = builder.create();
					alertDialog.show();
					return;
				
				}
				
			}
		});
	}
	
	
	
}
